package com.lighthouse.doc.comment.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import com.lighthouse.doc.base.dao.KnowledgeDocBaseInfoRepository;
import com.lighthouse.doc.base.domain.KnowledgeDocBaseInfo;
import com.lighthouse.doc.comment.domain.KnowledgeDocCommentVO;
import com.lighthouse.doc.operate.domain.KnowledgeDocOperateDTO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lighthouse.doc.comment.mapper.KnowledgeDocCommentMapper;
import com.lighthouse.doc.comment.domain.KnowledgeDocComment;
import com.lighthouse.doc.comment.service.IKnowledgeDocCommentService;

/**
 * 文档-用户评论关联Service业务层处理
 * 
 * @author ysz
 * @date 2023-05-10
 */
@Service
public class KnowledgeDocCommentServiceImpl implements IKnowledgeDocCommentService 
{
    @Autowired
    private KnowledgeDocCommentMapper knowledgeDocCommentMapper;

    @Autowired
    private KnowledgeDocBaseInfoRepository knowledgeDocBaseInfoRepository;

    /** 未删除的评论 */
    public static final String COMMENT_NOT_DELETE = "0";

    /**
     * 查询文档-用户评论关联
     * 
     * @param commentId 文档-用户评论关联主键
     * @return 文档-用户评论关联
     */
    @Override
    public KnowledgeDocComment selectKnowledgeDocCommentByCommentId(String commentId)
    {
        return knowledgeDocCommentMapper.selectKnowledgeDocCommentByCommentId(commentId);
    }

    /**
     * 查询文档-评论关联列表
     * @param
     * @return
     */
    @Override
    public List<KnowledgeDocCommentVO> selectKnowledgeDocCommentListByDocId(KnowledgeDocCommentVO knowledgeDocCommentVO)
    {
        knowledgeDocCommentVO.setIsDelete(COMMENT_NOT_DELETE);
        knowledgeDocCommentVO.setUserId(SecurityUtils.getUserId());
        return knowledgeDocCommentMapper.selectKnowledgeDocCommentListByDocId(knowledgeDocCommentVO);
    }

    /**
     * 新增文档-用户评论关联
     * 
     * @param knowledgeDocComment 文档-用户评论关联
     * @return 结果
     */
    @Override
    public int insertKnowledgeDocComment(KnowledgeDocComment knowledgeDocComment)
    {
        syncCommentToES(knowledgeDocComment);
        knowledgeDocComment.setCreateTime(LocalDateTime.now());
        knowledgeDocComment.setUserId(SecurityUtils.getUserId());
        knowledgeDocComment.setCommentId(UUID.randomUUID().toString());
        knowledgeDocComment.setIsDelete(COMMENT_NOT_DELETE);
        return knowledgeDocCommentMapper.insertKnowledgeDocComment(knowledgeDocComment);
    }

    /**
     * 同步ES评论的数据量
     */
    private void syncCommentToES(KnowledgeDocComment knowledgeDocComment) {
        KnowledgeDocBaseInfo knowledgeDocBaseInfo = knowledgeDocBaseInfoRepository.findById(knowledgeDocComment.getDocId()).orElse(null);

        knowledgeDocBaseInfo.setDocComments(knowledgeDocBaseInfo.getDocComments() + 1);
        knowledgeDocBaseInfoRepository.save(knowledgeDocBaseInfo);
    }

    /**
     * 修改文档-用户评论关联
     * 
     * @param knowledgeDocComment 文档-用户评论关联
     * @return 结果
     */
    @Override
    public int updateKnowledgeDocComment(KnowledgeDocComment knowledgeDocComment)
    {
        return knowledgeDocCommentMapper.updateKnowledgeDocComment(knowledgeDocComment);
    }

    /**
     * 批量删除文档-用户评论关联
     * 
     * @param commentIds 需要删除的文档-用户评论关联主键
     * @return 结果
     */
    @Override
    public int deleteKnowledgeDocCommentByCommentIds(String[] commentIds)
    {
        return knowledgeDocCommentMapper.deleteKnowledgeDocCommentByCommentIds(commentIds);
    }

    /**
     * 删除文档-用户评论关联信息
     * 
     * @param commentId 文档-用户评论关联主键
     * @return 结果
     */
    @Override
    public int deleteKnowledgeDocCommentByCommentId(String commentId)
    {
        return knowledgeDocCommentMapper.deleteKnowledgeDocCommentByCommentId(commentId);
    }
}
